﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ManageBanking.Models;
namespace ManageBanking.Controllers
{
    public class ChequeController : Controller
    {
        //
        // GET: /Cheque/

        ChequeModels cq = new ChequeModels();
        public ActionResult AddCheque(string idaccount,string money)
        {
            if (Session["Login"] != null && idaccount!= null && money !=null)
            {
                Username us = (Username)Session["Login"];
                if (us.IdTypeUser == 2)
                {
                    ViewData["menuleft_top"] = "DetailAccount";
                    decimal money1 = Convert.ToDecimal(money);
                    ViewData["Money"] = money1;
                    ViewData["IdAccount"] = idaccount;
                    ViewData["Res2"] = Session["Res2"];
                    ViewData["Res22"] = Session["Res22"];
                    Session.Remove("Res2");
                    Session.Remove("Res22");
                    return View();
                }
                else
                    return RedirectToAction("Home", "Login");
            
            }
            else
            return RedirectToAction("Home","Login");
        }
        public ActionResult DetailCheque()
        {   if (Session["Login"] != null)
            {
                Username us1 = (Username)Session["Login"];
                if (us1.IdTypeUser == 2)
                {
                    ViewData["menuleft_top"] = "ModifyCheque";
                    Username us = (Username)Session["Login"];
                    int total;
                    ViewData["Cheque"] = cq.ListCheque(us.IdUsername, out total);
                    ViewData["total"] = total == 0 ? 1 : total;
                    ViewData["Res3"] = Session["Res3"];
                    ViewData["Res4"] = Session["Res4"];
                    ViewData["Res5"] = Session["Res5"];
                    ViewData["Res6"] = Session["Res6"];

                    Session.Remove("Res3");
                    Session.Remove("Res4");
                    Session.Remove("Res5");
                    Session.Remove("Res6");
                    return View();
                }
                else
                    return RedirectToAction("Home", "Login");
            
            }
            else
            return RedirectToAction("Home","Login");
        
        }
        public ActionResult Delete(string id)
        {
            if (Session["Login"] != null && id != null)
            {
                Username us = (Username)Session["Login"];
                if (us.IdTypeUser == 2)
                {
                    if (cq.Delete(Convert.ToInt32(id)) == true)
                        Session["Res3"] = "Congarulation,Your Action Is Success";
                    else
                        Session["Res4"] = "Sorry,Your Cheque Is Transfered, You Can Not Stop";


                    return RedirectToAction("DetailCheque");
                }
                else
                    return RedirectToAction("Home", "Login");
            
            
            }
            else
                return RedirectToAction("Home","Login");
            
        }
        public ActionResult InformationCheque(string id)
        {
            
            if (Session["Login"] != null && id != null)
            {
                Username us = (Username)Session["Login"];
                if (us.IdTypeUser == 2)
                {
                    OnlineBankingDataContext olbk = new OnlineBankingDataContext();
                    Cheque a = olbk.Cheques.Where(ab => ab.IdCheque == Convert.ToInt32( id)).First();
                    DateTime now = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
                    if (a.DateReceiver < now)
                    {
                        return RedirectToAction("DetailCheque");
                    }
 
                    ViewData["info"] = cq.information(Convert.ToInt32(id));
                    ViewData["id"] = id;
                    return View();
                }
                else
                    return RedirectToAction("Home", "Login");

            }
            else
                return RedirectToAction("Home", "Login");

                           
        }
        public ActionResult ResolveChange(string id,string name,string address)
        {
            if (Session["Login"] != null && id != null && address != null)
            {
                Username us = (Username)Session["Login"];
                if (us.IdTypeUser == 2)
                {
                    if (cq.ChangeCheque(Convert.ToInt32(id), name, address) == true)
                        Session["Res5"] = "Congarulation,Your Action Is Success";
                    else
                        Session["Res6"] = "Sorry,Your Action Is Failed";
                    return RedirectToAction("DetailCheque");
                }
                else
                    return RedirectToAction("Home", "Login");
            }
            else
                return RedirectToAction("Home", "Login");
        }
        public ActionResult ResolveCheque(string totalmoney, string idusername, string idaccount, string name, string Address, string money, DateTime Date)
        {
            if (Session["Login"] != null && totalmoney != null && idusername != null & idaccount != null && name != null && Address != null && money != null && Date != null)
            {

                if (Convert.ToDecimal(totalmoney) - Convert.ToDecimal(money) < 100)
                {
                    Session["Res22"] = "Your Total Money Must Be More Than 100USD";
                    return RedirectToAction("AddCheque/" + idaccount + "/" + totalmoney);
                }
                DateTime dateto = Convert.ToDateTime(Date.ToString("yyyy-MM-dd"));
                DateTime datenow = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
                if (dateto <= datenow)
                {
                    Session["Res22"] = "Date Receiver must be Above Today";
                    return RedirectToAction("AddCheque/" + idaccount + "/" + totalmoney);
                }
                if (cq.AddCheque(Convert.ToInt32(idusername), idaccount, name, Address, Convert.ToDecimal(money), Date))
                {
                    decimal lessmony = Convert.ToDecimal(totalmoney) - Convert.ToDecimal(money);
                    Session["Res2"] = "";
                    Session.Remove("Res22");
                    return RedirectToAction("AddCheque/" + idaccount + "/" + lessmony);
                }
                else
                {
                    Session["Res22"] = "Error Connection";
                    return RedirectToAction("AddCheque");

                }
            }
            else
                return RedirectToAction("Home","Login");
        }
       
    }
}
